<template>
  <div id="login">
    <div>
      <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginFormCss">
        <h3 style="text-align: center">云e办 登录</h3>
        <el-form-item prop="username">
          <el-input type="text" auto-complete="false" v-model="loginForm.username" placeholder="请输入用户名"
                    clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" auto-complete="false" v-model="loginForm.password" placeholder="请输入密码"
                    clearable></el-input>
        </el-form-item>
        <el-form-item prop="captcha" style="text-align: left">
          <el-input style="width: 60%; float: left" type="text" auto-complete="false" clearable
                    v-model="loginForm.captcha" placeholder="点击更换验证码"></el-input>
          <img :src="captchaCodeUrl" @click="updateCaptchaCode" style="float: left; margin-left: 20px" width="120px"
               height="40px">
        </el-form-item>
        <el-checkbox style="margin-bottom: 20px" v-model="loginForm.rememberMe">记住我</el-checkbox>
        <el-button type="primary" auto-complete="false" style="width: 100%" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>

import {getRequest} from "@/utils/requestUtil";

export default {
  name: "Login",
  created() {
    this.keydo(this)
    this.updateCaptchaCode()
  },
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        rememberMe: true,
      },
      rules: {
        username: [{required: true, message: "请输入用户名", trigger: "blur"}],
        password: [{required: true, message: "请输入密码", trigger: "blur"}],
        captcha: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      captchaCodeUrl: "",
    }
  },
  methods: {
    keydo(ve) {
      document.onkeydown = (e) => {
        if (e.code == "Enter") {
          this.login()
        }
      }
    },
    //登录
    login() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      setTimeout(() => {
        loading.close();
      }, 2000);
      this.valid()
    },
    valid() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.postRequest(this.api.login, this.loginForm).then(res => {
            if (!res) {
              this.updateCaptchaCode()
              this.load = false
              return
            }
            //登录成功保存token
            window.sessionStorage.setItem("token_login", res.data.obj.tokenHead + res.data.obj.token)
            getRequest(this.api.getUserInfo).then(res => {
              if (res) {
                window.sessionStorage.setItem("user", JSON.stringify(res.data))
                //跳转首页
                let path = this.$route.query.redirect
                this.$router.replace((path == '/' || path == undefined) ? '/home' : path)
                this.load = false
              }
            })
          })
        } else {
          this.load = false
          return;
        }
      });
    },
    /*获取验证码*/
    updateCaptchaCode() {
      this.getRequest(this.api.captcha).then(res => {
        this.captchaCodeUrl = res.data.obj;
      })
    }
  },
  destroyed(){
    //取消键盘监听事件
    document.onkeydown = null
  },
}
</script>

<style scoped>
.loginFormCss {
  border-radius: 15px;
  margin: 10% auto;
  width: 30%;
  padding: 15px 35px 15px 35px;
  background: #F2F3F5;
  border: 1px solid #cac6c6;
  box-shadow: 0 0 25px #cac6c6;
}

#login{
  background: url(../assets/login-bj.jpg);
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
}
</style>